<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Admin\Controller;

/**
 * 学生课程计划
 *
 * @author gc
 */
class ProgrammeController extends AdminController
{

    public function index()
    {
        
    }

    public function add()
    {
        $id = I("id");

        $pay = M("Class_pay")->where("id=" . $id)->find();
        $class_start_time = $pay['class_start_time'];

        $student = D("Student")->getStudentById($pay['sid']);


        $list = M("class_plan")->where("pay_id=" . $pay['id'])->order("id asc")->select();

        //获取课程计划
        $map['course_id'] = $pay['course_id'];
        $map['status'] = 1;
        $plan = M("Course_plan")->field("id,course_id,explain,learning_content,teaching_emphasis,homework")->where($map)->order("gorder asc ")->select();

        if (empty($list)) {

            $tip = 0;
            $all = [];
            $class_hour = 1;
            foreach ($plan as $v) {
                if ($tip == 0) {
                    $tip = $v['id'] == $pay['class_start_id'] ? 1 : $tip;
                }

                if ($tip == 0) {
                    continue;
                }

                if ($pay['class_hour'] < $class_hour) {
                    break;
                }$class_hour++;

                $temp = [
                    'pay_id' => $pay['id'],
                    'course_plan_id' => $v['id'],
                    'content' => empty($v['explain']) ? "" : $v['explain'],
                    'learning_content' => empty($v['learning_content']) ? "" : $v['learning_content'],
                    'teaching_emphasis' => empty($v['teaching_emphasis']) ? "" : $v['teaching_emphasis'],
                    'homework' => empty($v['homework']) ? "" : $v['homework'],
                    'class_time' => $class_start_time,
                    'teacher_id' => UID,
                    'create_time' => time(),
                    'update_time' => time()
                ];
                $class_start_time += 7 * 86400;
                array_push($all, $temp);
            }

            M("class_plan")->addAll($all);
            $list = M("class_plan")->where("pay_id=" . $pay['id'])->order("id asc")->select();
        }

        $idarray = [];
        foreach ($list as $v) {
            if (!empty($v['course_plan_id'])) {
                array_push($idarray, $v['course_plan_id']);
            }
        }

        $this->assign('_list', $list);
        $this->assign('idarray', array_values($idarray));
        $this->assign('course_plan_ids', implode(',', $idarray));
        $this->assign('pay', $pay);
        $this->assign('plan', $plan);
        $this->assign('maxnum', count($list));



        $this->meta_title = $student['name'] . "《" . _getCourseTitle($pay['course_id']) . '》教学计划';
        $this->showTeacher();

        $this->display();
    }

    private function showTeacher()
    {
        $list = M("Member")->select();
        $this->assign("_teacher", $list);
    }

    public function ajaxUpdateClass_plan()
    {
        $course_plan_ids = I("course_plan_ids");
        $selected_ids = I("selected_ids");
        $payid = I("payid");

        $old_plan_id_array = explode(',', $course_plan_ids);
        $new_plan_id_array = explode(',', $selected_ids);


        $s = 0;
        if (count($old_plan_id_array) == count($new_plan_id_array) && !empty($payid)) {
            $map['pay_id'] = $payid;
            $model = M("class_plan");
            $s = $model->where($map)->delete();
            if ($s) {
                $map['id'] = ['in', $new_plan_id_array];
                $plan = M("Course_plan")->field("id,course_id,explain,learning_content,teaching_emphasis,homework")->where($map)->order("gorder asc ")->select();

                $pay = M("Class_pay")->where("id=" . $payid)->find();
                $class_start_time = $pay['class_start_time'];
                $all = [];
                foreach ($plan as $v) {
                    $temp = [
                        'pay_id' => $payid,
                        'course_plan_id' => $v['id'],
                        'content' => empty($v['explain']) ? "" : $v['explain'],
                        'learning_content' => empty($v['learning_content']) ? "" : $v['learning_content'],
                        'teaching_emphasis' => empty($v['teaching_emphasis']) ? "" : $v['teaching_emphasis'],
                        'homework' => empty($v['homework']) ? "" : $v['homework'],
                        'class_time' => $class_start_time,
                        'teacher_id' => UID,
                        'create_time' => time(),
                        'update_time' => time()
                    ];
                    $class_start_time += 7 * 86400;
                    array_push($all, $temp);
                }

                $s = M("class_plan")->addAll($all);
            }
        }

        $this->ajaxReturn(['status' => $s ? 1 : 0]);
    }

}
